package angry.courses.servlets.view;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import angry.courses.database.ICoursesDB;
import angry.courses.database.Student;
import angry.courses.servlets.HtmlFormatter;

public class ViewStudentsServlet extends HttpServlet {
	private ICoursesDB coursesDB;
    
    public void init() throws ServletException {
	coursesDB = (ICoursesDB) getServletContext().getAttribute("coursesDB");
    }

    public void destroy() {
	coursesDB = null;
    }
    
    public void doGet(HttpServletRequest req, HttpServletResponse res)
	throws  ServletException, IOException {
    	res.setContentType("text/html");
    	PrintWriter out = res.getWriter();
    	out.println(new HtmlFormatter() {
    	    public String title() {
    		return "View Students";
    	    }

       	    public void body(StringBuffer buf) {
       	    buf.append(heading(4, "Please choose the student you want to view (leave blank for all students)"));
       	    buf.append("<form method=\"post\" action=\"ViewStudents\">");
       	    buf.append("<table>");
       	    
    		buf.append(tr("Student name:", textbox("name"), button("Submit")));
    		buf.append("</table>");
    		buf.append("<br>Go " + link("back", "index.html") + " to main page.");
    	    }
    	}.htmlTag());
    	out.close();
    }
    
    public void doPost(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {
    	res.setContentType("text/html");
    	final String name = req.getParameter("name");
	    	
    	PrintWriter out = res.getWriter();
    	out.println(new HtmlFormatter() {
    	    public String title() {
    		return "View Students";
    	    }

       	    public void body(StringBuffer buf) {
	       	    if (!name.equals("")) {
	       	    	if (!addStudentToView(buf, this, name)) {
	       	    		buf.append(heading(4, "Student not found<br>"));
	       	    	}
	        	} else {
       	    		List<String> students = coursesDB.getStudentNames();
       	    		if (students != null) {
	       	    		for (String studentName : students) {
	       	    			addStudentToView(buf, this, studentName);
	       	    		}
       	    		} else {
       	    			buf.append(heading(4, "No student found<br>"));
       	    		}
	        	}
	       	    buf.append("Go " + link("back", "index.html") + " to main page.");
	       	    buf.append("<br>" + link("View", "ViewStudents") + " other students.");
       	    }   	    	
    	}.htmlTag());
    	out.close();
    }
    
    private boolean addStudentToView(StringBuffer buf, HtmlFormatter formater, String name) {
    	Student student = coursesDB.getStudent(name.trim());
    	if (student != null) {
	    	List<String> coursesList1 = coursesDB.getCoursesForStudent(name, 1);
	    	List<String> coursesList2 = coursesDB.getCoursesForStudent(name, 2);
	   	    completeLists(coursesList1, coursesList2);
	   	    buf.append("<b>");
	   	    buf.append(student.name + "<br>");
	   	    buf.append("Year of study: " + student.year + "<br>");
	   	    buf.append("</b>");
	   	    buf.append("<table border=\"1\">");
	   	    buf.append(formater.th("Semester 1") + formater.th("Semester2"));
	   	    for (int i = 0; i < coursesList1.size(); i++) {
	   	    	buf.append(formater.tr(300, coursesList1.get(i), coursesList2.get(i)));
	   	    }
	   	    buf.append("</table>");
	   	    buf.append("<br><br>");
	   	    // student was added
	   	    return true;
    	}
    	// student not added
    	return false;
    }

	private void completeLists(List<String> coursesList1,
			List<String> coursesList2) {
		int size1 = coursesList1.size();
		int size2 = coursesList2.size();
		if (size1 < size2) {
			for (int i = 0; i < size2 - size1; i++) {
				coursesList1.add("");
			}
		} else {
			for (int i = 0; i < size1 - size2; i++) {
				coursesList2.add("");
			}
		}
	}
}